package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IEmpEducationRequireDAO;
import org.jsola.hr.dto.EmpEducationRequireAddDTO;
import org.jsola.hr.dto.EmpEducationRequireUpdateDTO;
import org.jsola.hr.entity.CompanyDO;
import org.jsola.hr.entity.EmpEducationRequireDO;
import org.jsola.hr.query.EmpEducationRequireQuery;
import org.jsola.hr.service.ICompanyService;
import org.jsola.hr.service.IEmpEducationRequireService;
import org.jsola.hr.vo.EmpEducationRequireListVO;
import org.jsola.hr.vo.EmpEducationRequireVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrEmpEducationRequireServiceImpl")
public class EmpEducationRequireServiceImpl implements IEmpEducationRequireService {

    @Autowired
    private IEmpEducationRequireDAO empEducationRequireDAO;

    @Autowired
    private ICompanyService companyService;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpEducationRequireVO save(EmpEducationRequireAddDTO empEducationRequireAddDTO, TokenUser tokenUser) {
        // 校验是否存在
        EmpEducationRequireVO requireVO = findByCompanyAndType(
                empEducationRequireAddDTO.getCompanyId(),
                empEducationRequireAddDTO.getRequireType(),
                tokenUser.getSiteId());
        if (!Objects.isNull(requireVO)) {
            // 修改
            EmpEducationRequireUpdateDTO requireUpdateDTO = empEducationRequireAddDTO.to(EmpEducationRequireUpdateDTO.class);
            requireUpdateDTO.setId(requireVO.getId());

            update(requireUpdateDTO, tokenUser);
            return selectById(requireVO.getId(), tokenUser.getSiteId());
        }
        // 转DO
        EmpEducationRequireDO empEducationRequireDO = empEducationRequireAddDTO.to(EmpEducationRequireDO.class);
        // 保存
        empEducationRequireDO = save(empEducationRequireDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return empEducationRequireDO.to(EmpEducationRequireVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(EmpEducationRequireUpdateDTO empEducationRequireUpdateDTO, TokenUser tokenUser) {
        //转DO
        EmpEducationRequireDO empEducationRequireDO = empEducationRequireUpdateDTO.to(EmpEducationRequireDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(empEducationRequireDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... empEducationRequireIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) empEducationRequireIds);
    }


    @Override
    public EmpEducationRequireVO selectById(String empEducationRequireId, String siteId) {
        EmpEducationRequireDO empEducationRequireDO = selectDOById(empEducationRequireId, siteId);
        if (empEducationRequireDO == null) {
            return null;
        }
        return empEducationRequireDO.to(EmpEducationRequireVO.class);
    }

    @Override
    public List<EmpEducationRequireListVO> select(EmpEducationRequireQuery empEducationRequireQuery, String siteId) {
        List<EmpEducationRequireDO> empEducationRequireDOList = selectDO(empEducationRequireQuery, siteId);
        if(CollectionUtils.isEmpty(empEducationRequireDOList)) {
            return empEducationRequireDOList == null ? null : new ArrayList<>();
        }
        return empEducationRequireDOList.stream()
                .map(empEducationRequireDO -> empEducationRequireDO.to(EmpEducationRequireListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(EmpEducationRequireQuery empEducationRequireQuery, String siteId) {
        Example example = buildExample(empEducationRequireQuery,siteId);
        return empEducationRequireDAO.selectCountByExample(example);
    }

    @Override
    public Page<EmpEducationRequireListVO> selectPage(EmpEducationRequireQuery empEducationRequireQuery, String siteId) {
        Example example = buildExample(empEducationRequireQuery,siteId);
        Page<EmpEducationRequireDO> page = empEducationRequireDAO.selectPageByExample(example,
                empEducationRequireQuery.getPageNo(),
                empEducationRequireQuery.getPageSize());

        return page.to(EmpEducationRequireListVO.class);
    }

    @Override
    public EmpEducationRequireDO selectDOById(String empEducationRequireId, String siteId) {
        return listById(empEducationRequireId, siteId);
    }

    @Override
    public List<EmpEducationRequireDO> selectDO(EmpEducationRequireQuery empEducationRequireQuery, String siteId) {
        Example example = buildExample(empEducationRequireQuery,siteId);
        return empEducationRequireDAO.selectByExample(example);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmpEducationRequireVO selectByCompanyAndType(String companyId, String requireType, TokenUser tokenUser) {
        EmpEducationRequireVO requireVO = findByCompanyAndType(companyId, requireType, tokenUser.getSiteId());
        if (Objects.isNull(requireVO)) {
            CompanyDO company = companyService.selectDOById(companyId, tokenUser.getSiteId());
            if (Objects.isNull(company)) {
                return null;
            }
            // 没有初始化一条
            EmpEducationRequireAddDTO requireAddDTO = new EmpEducationRequireAddDTO();
            requireAddDTO.setCompanyId(companyId);
            requireAddDTO.setRequireType(requireType);

            requireVO = save(requireAddDTO, tokenUser);
        }
        return requireVO;
    }

    /**
     * 根据公司id和类型查询所需信息
     *
     * @param companyId   公司id
     * @param requireType 类型
     * @param siteId      站点
     * @return 所需信息
     */
    private EmpEducationRequireVO findByCompanyAndType(String companyId, String requireType, String siteId) {
        Example example = new Example(EmpEducationRequireDO.class);
        example.and()
                .andEqualTo("companyId", companyId)
                .andEqualTo("requireType", requireType)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);

        EmpEducationRequireDO requireDO = empEducationRequireDAO.selectOneByExample(example);
        if (Objects.isNull(requireDO)) {
            return null;
        }
        return requireDO.to(EmpEducationRequireVO.class);
    }


    /**
     * 根据查询参数，构建example

     * @param empEducationRequireQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(EmpEducationRequireQuery empEducationRequireQuery, String siteId) {
        Example example = new Example(EmpEducationRequireDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (empEducationRequireQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", empEducationRequireQuery.getCompanyId());
        }
        if (empEducationRequireQuery.getRequireType() != null) {
            example.and().andEqualTo("requireType", empEducationRequireQuery.getRequireType());
        }
        if (empEducationRequireQuery.getGraCollege() != null) {
            example.and().andEqualTo("graCollege", empEducationRequireQuery.getGraCollege());
        }
        if (empEducationRequireQuery.getIsLehramt() != null) {
            example.and().andEqualTo("isLehramt", empEducationRequireQuery.getIsLehramt());
        }
        if (empEducationRequireQuery.getMajor() != null) {
            example.and().andEqualTo("major", empEducationRequireQuery.getMajor());
        }
        if (empEducationRequireQuery.getEducation() != null) {
            example.and().andEqualTo("education", empEducationRequireQuery.getEducation());
        }
        if (empEducationRequireQuery.getTeaMethods() != null) {
            example.and().andEqualTo("teaMethods", empEducationRequireQuery.getTeaMethods());
        }
        if (empEducationRequireQuery.getStartTime() != null) {
            example.and().andEqualTo("startTime", empEducationRequireQuery.getStartTime());
        }
        if (empEducationRequireQuery.getEndTime() != null) {
            example.and().andEqualTo("endTime", empEducationRequireQuery.getEndTime());
        }
        if (empEducationRequireQuery.getIsFirstEdu() != null) {
            example.and().andEqualTo("isFirstEdu", empEducationRequireQuery.getIsFirstEdu());
        }
        if (empEducationRequireQuery.getIsHighestEdu() != null) {
            example.and().andEqualTo("isHighestEdu", empEducationRequireQuery.getIsHighestEdu());
        }
        if (empEducationRequireQuery.getDegreeName() != null) {
            example.and().andEqualTo("degreeName", empEducationRequireQuery.getDegreeName());
        }
        if (empEducationRequireQuery.getIsFirstDegree() != null) {
            example.and().andEqualTo("isFirstDegree", empEducationRequireQuery.getIsFirstDegree());
        }
        if (empEducationRequireQuery.getIsHighestDegree() != null) {
            example.and().andEqualTo("isHighestDegree", empEducationRequireQuery.getIsHighestDegree());
        }
        // 排序
        ExampleKit.setExampleOrder(example,empEducationRequireQuery.getOrders());
        return example;
    }
}




